package com.zhu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhu.domain.InventoryRecord;
import com.zhu.mapper.InventoryRecordMapper;
import com.zhu.service.InventoryRecordService;


import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author JoyBoy
 * @since 2022-06-02
 */
@Service
public class InventoryRecordServiceImpl extends ServiceImpl<InventoryRecordMapper, InventoryRecord> implements InventoryRecordService {

    @Override
    public List<InventoryRecord> getInventoryRecordByOperation(Integer operation) {
        QueryWrapper<InventoryRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("operation", operation);
        return this.list(queryWrapper);
    }

    @Override
    public InventoryRecord getRecordByRid(String rid) {
        QueryWrapper<InventoryRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rid", rid);
        return this.getOne(queryWrapper);
    }

    @Override
    public List<InventoryRecord> conditionSelect(Integer operation, String warehouseName, String rid) {
        QueryWrapper<InventoryRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("operation", operation);
        if (warehouseName != null) {
            queryWrapper.like("warehouse_name", warehouseName);
        }
        if (rid != null) {
            queryWrapper.like("rid", rid);
        }

        return this.list(queryWrapper);
    }

    @Override
    public boolean updateInventoryRecord(InventoryRecord inventoryRecord) {
        return this.updateById(inventoryRecord);
    }

    @Override
    public boolean updateRealName(String realName, String newName) {
        QueryWrapper<InventoryRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("real_name",realName);
        List<InventoryRecord> list = this.list(queryWrapper);
        for (InventoryRecord inventoryRecord :list){
            inventoryRecord.setRealName(newName);
        }
        return this.updateBatchById(list);
    }

    @Override
    public boolean updateWarehouseName(String oldName, String newName) {
        QueryWrapper<InventoryRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("warehouse_name",oldName);
        List<InventoryRecord> list = this.list(queryWrapper);
        for (InventoryRecord inventoryRecord :list){
            inventoryRecord.setWarehouseName(newName);
        }
        return this.updateBatchById(list);
    }

    @Override
    public boolean saveInventoryRecord(InventoryRecord inventoryRecord) {
        return this.save(inventoryRecord);
    }
}
